package controller.fileUploadDownload;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import properties.SystemProperty;
import util.io.UploadDownloadFileUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Administrator on 2017/10/27.
 */
@Controller
public class FileDownloadController {
    Logger log = LoggerFactory.getLogger(FileDownloadController.class);
////    @RequiresUser
//    @RequestMapping("downloadFile")
//    public ResponseEntity<byte[]> downloadFile(@RequestParam(value = "type") Integer type,String  id ,HttpServletRequest request) throws IOException {
//        String pathName = null;
//        String newName = null;
//        if (type == 1) {
//            //通知文件下载
////            NoticeInfo notice = noticeService.seByPk(pk);
////            pathName = notice.getFileSrc();
////            newName = notice.getFileName();
//        } else if (type == 2) {
//            //专家证书
////            ExpertCertificate expertCertificate = expertService.getExpertCertificateByPk(pk);
////            pathName = expertCertificate.getImgSrc();
////            newName = pathName;
//        } else if (type == 3) {
////            //头像
////            pathName = "expertHeadImg\\default.png ";
////            if (pk != null) {
////                ExpertInfo expertInfo = expertService.selByPk(pk);
////                if (expertInfo != null && expertInfo.getHeadImg() != null) {
////                    pathName = expertInfo.getHeadImg();
////                }
////            }
////            newName = pathName;
//        } else if (type == 4) {
//            //专家公告导入模板
//            pathName = SystemProperty.getExcelTemplateFolder() + File.separator + "trainTemplate.xls";
//            newName = "专家公告导入模板.xls";
//        } else if (type == 5) {
//            //专家信息导入模板
//            pathName = SystemProperty.getExcelTemplateFolder() + File.separator + "expertInfoTemplate.rar";
//            newName = "Excel导入专家模板.rar";
//        }  else {
//            return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
//        }
//
//        log.info("pathName:{};newName:{}", pathName, newName);
//        ResponseEntity<byte[]> entity = UploadDownloadFileUtil.downFileByRelativePathName(SystemProperty.getLocalFilePath(),pathName, newName, request);
//        if (entity != null) {
//            return entity;
//        } else {
//            return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
//        }
//    }
//    pathName=excelTemplate/expertInfoTemplate.rar&newName=Excel导入专家模板.rar
//    pathName=excelTemplate/trainTemplate.xls&newName=专家公告（培训）导入模板.xls

//    @RequiresUser
    @RequestMapping("downloadFileByPath")
    public ResponseEntity<byte[]> downloadFileByPath(HttpServletRequest request,@RequestParam(value = "pathName") String pathName, String newName) throws IOException {
        if (pathName == null) {
            return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
        }
        if (newName == null || "".equals(newName)) {
            newName = pathName.substring(pathName.lastIndexOf("/")+1);
        }

        pathName = pathName.replace('/', '\\');
        log.info("pathName:{};newName:{}", pathName, newName);
        ResponseEntity<byte[]> entity = UploadDownloadFileUtil.downFileByRelativePathName(SystemProperty.getLocalFilePath(),pathName, newName, request);
        if (entity != null) {
            return entity;
        } else {
            return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
        }
    }




}
